<template>
<view>
<!-- 富文本 -->


<view class="newContainer" v-if="newListData">
    <view class="newsTitle"><text>{{newListData.title}}</text></view>
    <view class="newsSubTitle">
      <image class="logo" :src="setting.platformSetting.logo||'https://image1.sansancloud.com/jianzhan/2018_7/6/10/33/31_45.jpg'"></image>
      <view class="group">
        <view class="newsType">{{newListData.typeName}}</view>
        <view class="releaseTime">{{newListData.publishTime}}</view>
      </view>
      <view class="shareFormPoster" @tap="showShare">
        <image class="shareFormPosterImg" src="https://image1.sansancloud.com/xianhua/2019_3/21/16/30/34_113.jpg?x-oss-process=style/preview_120"></image>
      </view>
    </view>
    <view class="wxParseData">
      <rich-text-component :receiveData="articleJsonData" v-if="articleJsonData"></rich-text-component>
      <!-- <rich-text nodes='{{newListData.content}}'></rich-text> -->
    </view>
</view>
<custom-page-index-component :receiveData="sendNewsData" v-if="sendNewsData"></custom-page-index-component>
<!-- 首页组件-公共新闻详情装饰-->
<custom-page-index-component :receiveData="sendIndexData" v-if="sendIndexData"></custom-page-index-component>
    <!--  #ifdef MP-WEIXIN -->
<!-- <view class="bottomBar">
  <button open-type="share" class="shareToFriends">
    <image class="shareToFriendsIcon" src="https://image1.sansancloud.com/jianzhan/2018_12/14/14/30/55_49.jpg"></image>
    <text>分享给好友</text>
  </button>
  <view class="createPoster" @tap="showPoster">
    <image class="createPosterIcon" src="https://image1.sansancloud.com/jianzhan/2018_12/14/14/30/55_324.jpg"></image>
    <text>生成海报</text>
  </view>
</view> -->
<!-- 分享 -->
<view class="zhezhao" v-if="showTypeTwo" @tap="closeZhezhao"></view>
<view class="select_share_list" :animation="animationDataTwo">
  <view class="select_share_item" v-for="(item, index) in shareTypeData" :key="index">
    <text class="select_share_name" @click.stop="showPoster" v-if="item.type=='text'">{{item.name}}</text>
    <button open-type="share" class="select_share_name" v-else>{{item.name}}</button>
  </view>
  <view class="select_share_item cancel_select_share" @tap="closeZhezhao">
    <text class="select_share_name">取消</text>
  </view>
</view>
    <!--  #endif -->
<news-poster-component :newsId="params.id||params.newsId" :ewmImgUrl="ewmImgUrl" v-if="posterState" @closePoaster="getChilrenPoster"></news-poster-component>
</view>
</template>

<script>


export default {
  data() {
    return {
      newListData: null,
      params: {},
      ewmImgUrl: "",
      onloadOpt: {},
      posterState: false,
      setting: "",
      sendIndexData: "",
      sendNewsData: "",
      articleJsonData: "",
      showTypeTwo: false,
      animationDataTwo: null,
      shareTypeData: [{
        name: '发送给朋友',
        type: 'botton'
      }, {
        name: '生成海报',
        type: 'text'
      }],
    };
  },

  props: {},
  onLoad: function (options) {
    let that = this;
	that.params=options
    that.setData({
      params: options,
      setting: getApp().globalData.setting
    });
    console.log('options', options);
    let sendIndexData = JSON.stringify({
      title: 'noTitle',
      url: "news",
      params: {
        pageObjectId: options.id || options.newsId||''
      }
    });
    that.setData({
      sendIndexData: sendIndexData
    });
    let sendNewsData = JSON.stringify({
      title: 'noTitle',
      url: "news_detail_" + (options.id || options.newsId)
    });
    that.setData({
      sendNewsData: sendNewsData
    });
    that.getData(that.params);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this;
    console.log("下拉");
	that.getData(that.params);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareTimeline:function(){
    return this.shareFun();
  },
  onShareAppMessage: function (res) {
    console.log(res);
    return this.shareFun();
  },
  methods: {
    shareFun:function(){
        let that = this;
        let params = that.onloadOpt;
        console.log('params:' , params);
        return getApp().globalData.shareForFx2('news_detail', that.newListData.title, params);
    },
    // 关闭海报
    closeZhezhao: function () {
      this.setData({
        showTypeTwo: false
      });
      let animation = wx.createAnimation({
        duration: 400,
        timingFunction: 'ease-out'
      });
      animation.height(0).step();
      let setData = animation.export();
      this.setData({
        animationDataTwo: setData
      });
    },
    showShare: function () {
      this.setData({
        showTypeTwo: !this.showTypeTwo
      });
      let showTypeTwo = this.showTypeTwo;
      let animation = wx.createAnimation({
        duration: 400,
        timingFunction: 'ease-in-out'
      });
      console.log("=======popupFormPage==========", animation, this.showType);

      if (showTypeTwo) {
        animation.height(150).step();
      } else {
        animation.height(0).step();
      }

      this.setData({
        animationDataTwo: animation.export()
      });
    },
    getChilrenPoster(e) {
      let that = this;
      that.setData({
        posterState: false
      });
    },

    showPoster: function () {
      let that = this;
      console.log('===showPoster====', that.onloadOpt.id);
      if (getApp().globalData.loginUser && getApp().globalData.loginUser.platformUser.id) {
        let ewmImgUrl = getApp().globalData.getQrCode({
          type: "news_detail",
          id: that.onloadOpt.id || that.onloadOpt.newsId
        });
        that.setData({
          posterState: true,
          ewmImgUrl: ewmImgUrl
        });
      } else {
        wx.showModal({
          title: '提示',
          content: '您还未登录，请先登入~',
          success: function (res) {
            if (res.confirm) {
              getApp().globalData.linkEvent("login.html")
            } else if (res.cancel) {}
          }
        });
      }
    },
    getData: function (e) {
      var that = this;
      var getParams = {};
      getParams.newsId = e.id || e.newsId;
      var customIndex = getApp().globalData.AddClientUrl("/get_news_bbs_detail.html", getParams); 
      getApp().globalData.showToastLoading('loading', true); //拿custom_page
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log(res.data);
          //WxParse.wxParse('article', 'html', res.data.content, that, 10)
		  let articleJsonData=res.data.content;
		  wx.setNavigationBarTitle({
		    title: res.data.title||'新闻详情'
		  });
		  that.setData({
		    articleJsonData:{jsonData:{content:articleJsonData}}
		  });
          // setTimeout(function () {
          //   that.richText = res.data.content;
          // });
          // console.log('===0000=====', that.article.nodes); 

          that.setData({
            newListData : res.data
          });
          wx.hideLoading();
        },
        fail: function (res) {
          wx.hideLoading();
          getApp().globalData.loadFail();
        }
      });
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>